﻿using System.Collections;

using UnityEngine;
//继承MovingObject类
public class Player : MovingObject {

    public int wallDamage = 1; //对墙壁的伤害
    public int pointsPerFood = 10; //食物的回复量
    public int pointsPerSoda = 20; //苏打水的回复量
    public float restartlevelDelay = 1f; //进入下一关的时间差

    private Animator animator; //PlayerChop, PlayerHit用
    private int food; //Player体力

    //继承MovingObject的Start方法　用base调用
    protected override void Start ( ) {
        //获取Animator
        animator = GetComponent<Animator> ( );
        //从单例的GameManager中获取playerFoodPoints
        //即使跨越场景（关卡）也能保持变量
        food = GameManager.instance.playerFoodPoints;
        //调用MovingObject的Start
        base.Start ( );
    }
    //Player脚本无效之前、体力保存至GameManager
    //Unity的生命周期API方法
    private void OnDisable ( ) {
        GameManager.instance.playerFoodPoints = food;
    }

    void Update ( ) {
        //Player回合之外Update的处理直接跳出
        if (!GameManager.instance.playersTurn)
            return;

        int horizontal = 0; //-1: 左移动, 1: 右移动
        int vertical = 0; //-1: 下移动, 1: 上移动

        horizontal = (int) Input.GetAxisRaw ("Horizontal");
        vertical = (int) Input.GetAxisRaw ("Vertical");
        //同时输入上下与左右的时候限制其中一方(限制移动方向要么横向要么纵向)
        if (horizontal != 0) {
            vertical = 0;
        }
        //无论进行上下左右移动之时
        if (horizontal != 0 || vertical != 0) {
            //Wall: 指定传入范型<T>类
            //Player的场合只需要判定Wall
            AttemptMove<Wall> (horizontal, vertical);
        }
    }

    protected override void AttemptMove<T> (int xDir, int yDir) {
        //移动1回减少1个食物
        food--;
        //调用MovingObject的AttemptMove
        base.AttemptMove<T> (xDir, yDir);

        RaycastHit2D hit;

        CheckIfGameOver ( );
        //Player回合结束
        GameManager.instance.playersTurn = false;
    }

    //必须复写MovingObject的抽象方法
    protected override void OnCantMove<T> (T component) {
        //用Wall型用来反射获取 Wall脚本
        Wall hitWall = component as Wall;
        //调用Wall脚本的DamageWall方法
        hitWall.DamageWall (wallDamage);
        //触发对Wall攻击的动画
        animator.SetTrigger ("PlayerChop");
    }

    private void OnTriggerEnter2D (Collider2D other) {
        if (other.tag == "Exit") {
            //Invoke: 根据参数延迟执行方法
            Invoke ("Restart", restartlevelDelay);
            enabled = false; //使Player脚本无效(禁止操作)
        } else if (other.tag == "Food") {
            //体力回复删除other对象
            food += pointsPerFood;
            other.gameObject.SetActive (false);
        } else if (other.tag == "Soda") {
            //体力回复删除other对象
            food += pointsPerSoda;
            other.gameObject.SetActive (false);
        }
    }

    private void Restart ( ) {
        //读取相同的场景
        Application.LoadLevel (Application.loadedLevel);
    }
    //敌方角色对Player攻击时执行的方法
    public void LoseFood (int loss) {
        animator.SetTrigger ("PlayerHit");
        food -= loss;
        CheckIfGameOver ( );
    }

    private void CheckIfGameOver ( ) {
        if (food <= 0) {
            //执行GameManager的GameOver方法
            GameManager.instance.GameOver ( );
        }
    }
}
